package cn.edu.ujn.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import cn.edu.ujn.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import cn.edu.ujn.system.domain.FBuilding;
import cn.edu.ujn.system.mapper.FFacilityMapper;
import cn.edu.ujn.system.domain.FFacility;
import cn.edu.ujn.system.service.IFFacilityService;
import cn.edu.ujn.common.core.text.Convert;

/**
 * 设备信息管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-22
 */
@Service
public class FFacilityServiceImpl implements IFFacilityService 
{
    @Autowired
    private FFacilityMapper fFacilityMapper;

    /**
     * 查询设备信息管理
     * 
     * @param facilityId 设备信息管理主键
     * @return 设备信息管理
     */
    @Override
    public FFacility selectFFacilityByFacilityId(Long facilityId)
    {
        return fFacilityMapper.selectFFacilityByFacilityId(facilityId);
    }

    /**
     * 查询设备信息管理列表
     * 
     * @param fFacility 设备信息管理
     * @return 设备信息管理
     */
    @Override
    public List<FFacility> selectFFacilityList(FFacility fFacility)
    {
        return fFacilityMapper.selectFFacilityList(fFacility);
    }

    /**
     * 新增设备信息管理
     * 
     * @param fFacility 设备信息管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFFacility(FFacility fFacility)
    {
        int rows = fFacilityMapper.insertFFacility(fFacility);
        insertFBuilding(fFacility);
        return rows;
    }

    /**
     * 修改设备信息管理
     * 
     * @param fFacility 设备信息管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFFacility(FFacility fFacility)
    {
        fFacilityMapper.deleteFBuildingByBuildingId(fFacility.getFacilityId());
        insertFBuilding(fFacility);
        return fFacilityMapper.updateFFacility(fFacility);
    }

    /**
     * 批量删除设备信息管理
     * 
     * @param facilityIds 需要删除的设备信息管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFFacilityByFacilityIds(String facilityIds)
    {
        fFacilityMapper.deleteFBuildingByBuildingIds(Convert.toStrArray(facilityIds));
        return fFacilityMapper.deleteFFacilityByFacilityIds(Convert.toStrArray(facilityIds));
    }

    /**
     * 删除设备信息管理信息
     * 
     * @param facilityId 设备信息管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFFacilityByFacilityId(Long facilityId)
    {
        fFacilityMapper.deleteFBuildingByBuildingId(facilityId);
        return fFacilityMapper.deleteFFacilityByFacilityId(facilityId);
    }

    /**
     * 新增宿舍楼信息管理信息
     * 
     * @param fFacility 设备信息管理对象
     */
    public void insertFBuilding(FFacility fFacility)
    {
        List<FBuilding> fBuildingList = fFacility.getFBuildingList();
        Long facilityId = fFacility.getFacilityId();
        if (StringUtils.isNotNull(fBuildingList))
        {
            List<FBuilding> list = new ArrayList<FBuilding>();
            for (FBuilding fBuilding : fBuildingList)
            {
                fBuilding.setBuildingId(facilityId);
                list.add(fBuilding);
            }
            if (list.size() > 0)
            {
                fFacilityMapper.batchFBuilding(list);
            }
        }
    }
}
